<?php
// 应用公共文件

/*name select 标签*/
/*n_id 选中的option*/
function getSelect($arr,$title='name',$sname='',$n_id='0'){
		$html .='<select name="' . $name . '">';
		foreach($arr as $row){
			if($row['id'] == $n_id){
				$html .= '<option value="' . $row['id'] . '"selected>' . $row[$title]. '</option>';
			}else{
				$html .= '<option value="' . $row['id'] . '">' . $row[$title]. '</option>';
			}
		}
		$html .= '</select>';
		return $html;
}
/*菜单生成调用*/
function getTree($list,$child = 'child', $pk='id',$pid = 'parentId',$root=0)
{    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
			 if(isset($list[$key]['control_name'])){
				$tmp = explode('?',$list[$key]['action_name']);
				if(count($tmp)>1){
					$refer[$data[$pk]]['url'] = url($list[$key]['control_name'] . '/' . $list[$key]['action_name'],$tmp[1]);
				 }else{
					$refer[$data[$pk]]['url'] = url($list[$key]['control_name'] . '/' . $list[$key]['action_name']); 
				 }
					 
			 }
        }
        foreach ($list as $key => $data) {
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];  
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
	                $parent[$child][] =& $list[$key];                 
                }
            }
        }
    }
    return $tree;
}
//$old_time = '2018-07-07';  
//echo get_time_format($old_time);
//1天前
function get_time_format($old_time){
    if (0 == intval($old_time)){
        return '很久前';
    }
    else{
        $now_time = new DateTime(date("Y-m-d H:i:s"));     
        $old_time = new DateTime($old_time); 
        $num = $now_time->diff($old_time);  
        $num->invert == 0 ? $str = '后' : $str = '前';
        switch ($num){
            case $num->y > 0:
                $data_y = $num->format('%y年');  
                $res = $data_y . $str;
                break;
            case $num->m > 0:
                $data_y = $num->format('%m月');  
                $res = $data_y . $str;
                break;
            case $num->d > 0:
                $data_y = $num->format('%d天');  
                $res = $data_y . $str;
                break;
            case $num->h > 0:
                $data_y = $num->format('%h小时');  
                $res = $data_y . $str;
                break;
            case $num->i > 0:
                $data_y = $num->format('%I分钟');  
                $res = $data_y . $str;
                break;
            case $num->s > 0:
                $data_y = $num->format('%S秒');  
                $res = $data_y . $str;
                break;
            default:
                $res = '很久前';
                break;
        }
    }
    return $res;
}